package com.dongdongshop.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;

import javax.annotation.PostConstruct;

@Configuration
public class BeanConfig {

    @PostConstruct
    private void initBlockHandler() {
        BlockRequestHandler br = (serverWebExchange, throwable) -> {
            Result r = Result.response(ResultEnums.TOO_MANY_REQUEST);
            return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(r));
        };
        GatewayCallbackManager.setBlockHandler(br);
    }
}
